﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using Meqs.Logic.Interface;
using Meqs.Entity;
using System.Collections.Generic;
using System.Web.Services;

public partial class Score_School_ForManage_Main : System.Web.UI.Page
{

    protected bool canDomanage = true;
    protected IList<ExamForArea> examforarea;
    protected IList<GlobalCauseInfo> schoolglobalCause = new List<GlobalCauseInfo>();
    protected int Grade
    {
        get
        {
            string grade = HttpContext.Current.Request.QueryString["grade"];
            int temp = 0;
            if (int.TryParse(grade, out temp))
            {
                return temp;
            }
            return 0;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
            schoolyearTxt.Value = si.SchoolYear.ToString();
            termTxt.Value = si.SchoolTerm.ToString();
            gradeTxt.Value = Grade.ToString();
        }
    }
    [WebMethod]
    public static int SaveAreaInfo(int year, int term, int examtype, int grade,int[] ids,float[] scores)
    {
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", Common.GetConstSchoolMark());
        ht.Add("SchoolYear", year);
        ht.Add("Term", term);
        ht.Add("Grade", grade);
        ht.Add("Part", examtype);
        Container<IExamForAreaLogic>.Instance.DeleteExamAreaInfo(ht);

        for (int i = 0; i < ids.Length; i++)
        {
            ht = new Hashtable();
            ht.Add("SchoolMark",Common.GetConstSchoolMark());
            ht.Add("Grade", grade);
            ht.Add("SchoolYear", year);
            ht.Add("Term", term);
            ht.Add("Part", examtype);
            ht.Add("CauseID", ids[i]);
            double avg = Container<IExamForAreaLogic>.Instance.GetAvgInfo(ht);
            
            ExamForArea earear = new ExamForArea();
            earear.AreaAvg = (double)scores[i];
            earear.BValue = 0;
            if (scores[i] != 0)
            {
                earear.BValue =(double) (avg / (double)scores[i]);
            }

            earear.SchoolAvg = avg;
            earear.CauseID = ids[i];
            earear.Grade = grade;
            earear.Part = examtype;
            earear.SchoolMark = Common.GetConstSchoolMark();
            earear.SchoolYear = year;
            earear.Status = 0;
            earear.Term = term;
            Container<IExamForAreaLogic>.Instance.Create(earear);
        }
        

        return 1;
    }
    [WebMethod]
    public static string GetExamAreaInfo(int year, int term, int examtype, int grade)
    {
        string str = "";
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", Common.GetConstSchoolMark());
        ht.Add("SchoolYear", year);
        ht.Add("Term", term);
        ht.Add("Grade", grade);
        ht.Add("Part", examtype);
        IList<ExamForArea>  examforarea = Container<IExamForAreaLogic>.Instance.FindAll(ht);

        Hashtable htParam = new Hashtable();
        htParam.Add("IsEffect", 1);
        List<GlobalCauseInfo> globalCause = (List<GlobalCauseInfo>)Container<IGlobalCauseInfoLogic>.Instance.FindAll(htParam);

        ht = new Hashtable();
        ht.Add("SchoolMark", Common.GetConstSchoolMark());
        List<CauseInfo> SchoolCause = (List<CauseInfo>)Container<ICauseInfoLogic>.Instance.FindAll(ht);

        ht = new Hashtable();
        ht.Add("SchoolMark", Common.GetConstSchoolMark());
        ht.Add("Grade", grade);
        IList<CouseGradeInfo> couinfo = Container<ICouseGradeInfoLogic>.Instance.FindAll(ht);
        IList<GlobalCauseInfo> schoolglobalCause = new List<GlobalCauseInfo>();
        foreach (GlobalCauseInfo g in globalCause)
        {
            if (CheckIn(SchoolCause, g.RID))
            {
                bool add = false;
                foreach (CouseGradeInfo cgi in couinfo)
                {
                    if (cgi.CourseID == g.RID)
                    {
                        add = true;
                        break;
                    }
                }
                if (add)
                {
                    schoolglobalCause.Add(g);
                }
            }
        }
        str += "<table id='awardTable' style='width: 95%;' class='useList'>";
        str += "<tr><th>选择</th><th>科目</th><th>区均分</th></tr>";
        foreach (GlobalCauseInfo g in schoolglobalCause)
        {
            str += "<tr>";
            //double areaba = 0;
            double areaavg = 0;
            bool isget = false;
            foreach (ExamForArea area in examforarea)
            {
                if (g.RID == area.CauseID )
                {
                    isget = true;
                    areaavg = Convert.ToDouble( area.AreaAvg);
                    break;
                }
            }
            if (isget)
            {
                str += "<td><input type='checkbox' id='c" + g.RID + "' name='subjs' onclick='OnClickCB(" + g.RID + ")' checked /></td>";
                str += "<td>" + g.CauseName + "</td>";
                str += "<td><input type='text' maxlength='7' id='textc" + g.RID + "' subjid='" + g.RID + "'  value='" + Math.Round(areaavg, 3) + "' oldvalue='" + Math.Round(areaavg, 3) + "' onfocusout='optionSettingScore(this)' /></td>";
            }
            else
            {
                str += "<td><input type='checkbox' id='c" + g.RID + "' name='subjs' onclick='OnClickCB(" + g.RID + ")' /></td>";
                str += "<td>" + g.CauseName + "</td>";
                str += "<td><input type='text' maxlength='7' id='textc" + g.RID + "' subjid='" + g.RID + "' value='' oldvalue='' onfocusout='optionSettingScore(this)'  disabled /></td>";
            }

            str += "<tr>";
        }
        str += "</table>";
        return str;
    }
    protected void btnBind_Click(object sender, EventArgs e)
    {
        if (SchoolCommon.CheckRole((int)RoleKeyEnum.Teach_Director))
        {
            canDomanage = true;
        }
        else
        {
            canDomanage = false;
        }
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", Common.GetConstSchoolMark());
        ht.Add("SchoolYear", Convert.ToInt32(schoolyearTxt.Value));
        ht.Add("Term", Convert.ToInt32(termTxt.Value));
        ht.Add("Grade", Convert.ToInt32(gradeTxt.Value));
        examforarea = Container<IExamForAreaLogic>.Instance.FindAll(ht);

        Hashtable htParam = new Hashtable();
        htParam.Add("IsEffect", 1);
        List<GlobalCauseInfo> globalCause = (List<GlobalCauseInfo>)Container<IGlobalCauseInfoLogic>.Instance.FindAll(htParam);
       
        ht = new Hashtable();
        ht.Add("SchoolMark", Common.GetConstSchoolMark());
        List<CauseInfo> SchoolCause = (List<CauseInfo>)Container<ICauseInfoLogic>.Instance.FindAll(ht);

        ht = new Hashtable();
        ht.Add("SchoolMark", Common.GetConstSchoolMark());
        ht.Add("Grade", Convert.ToInt32(gradeTxt.Value));
        IList<CouseGradeInfo> couinfo = Container<ICouseGradeInfoLogic>.Instance.FindAll(ht);
        foreach (GlobalCauseInfo g in globalCause)
        {
            if (CheckIn(SchoolCause, g.RID))
            {
                bool add = false;
                foreach (CouseGradeInfo cgi in couinfo)
                {
                    if (cgi.CourseID == g.RID)
                    {
                        add = true;
                        break;
                    }
                }
                if (add)
                {
                    schoolglobalCause.Add(g);
                }
            }
        }

        IList<ForExamInfo> forexam = new List<ForExamInfo>();
        SetExamInfo(forexam);
        Repeater1.DataSource = forexam;
        Repeater1.DataBind();
    }
    private static bool CheckIn(List<CauseInfo> SchoolCause, int causeid)
    {
        foreach (CauseInfo c in SchoolCause)
        {
            if (c.RID == causeid)
                return true;
        }
        return false;
    }
    protected string GetBValue(int typeid)
    {
        string str = "";
        int index = 0;
        foreach (GlobalCauseInfo g in schoolglobalCause)
        {
            foreach (ExamForArea area in examforarea)
            {
                if (g.RID == area.CauseID && typeid == area.Part)
                {
                    str += g.CauseName + ":" + Math.Round((double)area.BValue, 2) + " ";
                    index++;
                    if (index % 3 == 0)
                        str += "<br/>";
                }
            }
        }
        return str;
    }
    private void SetExamInfo(IList<ForExamInfo> forexam)
    {
        int year = Convert.ToInt32(schoolyearTxt.Value);
        int term = Convert.ToInt32(termTxt.Value);
        int grade = Convert.ToInt32(gradeTxt.Value);
        switch (grade)
        {
            case 7:
                {
                    if (term == 1)
                    {
                        //上学期
                        ForExamInfo exam = new ForExamInfo(year, term, grade, 0);
                        forexam.Add(exam);
                    }
                    ForExamInfo temp = new ForExamInfo(year, term, grade, 1);
                    forexam.Add(temp);
                    temp = new ForExamInfo(year, term, grade, 2);
                    forexam.Add(temp);
                }
                break;
            case 8:
                {
                    ForExamInfo temp = new ForExamInfo(year, term, grade, 1);
                    forexam.Add(temp);
                    temp = new ForExamInfo(year, term, grade, 2);
                    forexam.Add(temp);
                }
                break;
            case 9:
                {
                    if (term == 1)
                    {
                        //上学期
                        ForExamInfo temp = new ForExamInfo(year, term, grade, 1);
                        forexam.Add(temp);
                        temp = new ForExamInfo(year, term, grade, 2);
                        forexam.Add(temp);
                    }
                    else
                    {
                        //下学期
                        ForExamInfo temp = new ForExamInfo(year, term, grade, -2);
                        forexam.Add(temp);
                        temp = new ForExamInfo(year, term, grade, -3);
                        forexam.Add(temp);
                        temp = new ForExamInfo(year, term, grade, -4);
                        forexam.Add(temp);
                        temp = new ForExamInfo(year, term, grade, -5);
                        forexam.Add(temp);
                        temp = new ForExamInfo(year, term, grade, 6);
                        forexam.Add(temp);

                        temp = new ForExamInfo(year, term, grade, 4);
                        forexam.Add(temp);
                    }
                }
                break;
        }      
    }

    protected string GetExamName(int examtype)
    {
        string name = "";
        if (examtype < 0)
        {
            int month = Math.Abs(examtype);
            name = month + "月调考";
        }
        else
        {
            switch (examtype)
            { 
                case 0:
                    name = "入学考试";
                    break;
                case 1:
                    name = "期中考试";
                    break;
                case 2:
                    name = "期末考试";
                    break;
                case 3:
                    name = "小考";
                    break;
                case 4:
                    name = "中考";
                    break;
                case 5:
                    name = "高考";
                    break;
                case 6:
                    name = "模拟考试";
                    break;
            }
        }
        return name;
    }
}
[Serializable]
public class ForExamInfo
{
    public ForExamInfo(int schyear, int schterm, int schgrade,int schexamtype)//,int schexamparam
    {
        schoolyear = schyear;
        term = schterm;
        grade = schgrade;
        examtype = schexamtype;
        //examparam = schexamparam;
    }
    private int schoolyear;
    public int SchoolYear
    {
        get { return schoolyear; }
        set { schoolyear = value; }
    }
    private int term;
    public int Term
    {
        get { return term; }
        set { term = value; }
    }
    private int grade;
    public int Grade
    {
        get { return grade; }
        set { grade = value; }
    }

    private int examtype;
    public int ExamType
    {
        get { return examtype; }
        set { examtype = value; }
    }

    //private int examparam;
    //public int ExamParam
    //{
    //    get { return examparam; }
    //    set { examparam = value; }
    //}


}